package com.shou.infrastructure.persistent.repository.park;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shou.domain.common.PageQueryEntity;
import com.shou.domain.common.ParkingSpaceInfoEntity;
import com.shou.domain.park.model.ParkingSpaceEntity;
import com.shou.domain.park.repository.IParkingSpaceRepository;
import com.shou.infrastructure.persistent.dao.IParkingSpaceDao;
import com.shou.infrastructure.persistent.po.ParkingSpacePO;
import com.shou.infrastructure.persistent.repository.park.mapstruct.ParkingSpacePOConvert;
import jakarta.annotation.Resource;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
* @author Nemo
* @description 针对表【parkingspaces(车位表，记录每个车位的状态和相关信息)】的数据库操作Service
* @createDate 2025-02-07 22:10:51
*/
@Repository("parkingSpaceRepository")
public class ParkingSpaceRepository implements IParkingSpaceRepository {
    @Resource
    private IParkingSpaceDao parkingSpaceDao;
    @Resource
    private ParkingSpacePOConvert parkingSpacePOConvert;
    @Override
    public void insert(ParkingSpaceEntity parkingSpace) {
        ParkingSpacePO parkingSpacePO = parkingSpacePOConvert.convertParkingSpaceEntityToPO(parkingSpace);
        try {
            parkingSpaceDao.insertSelective(parkingSpacePO);
        }catch (DataIntegrityViolationException e) {
            Throwable rootCause = e.getRootCause();
            if (rootCause instanceof SQLException && "22001".equals(((SQLException) rootCause).getSQLState())) {
                throw new IllegalArgumentException("输入数据过长导致SQL执行失败");
            } else {
                throw new IllegalArgumentException(e.getMessage());
            }
        }
//        }  catch (DataIntegrityViolationException e) {
//            Throwable rootCause = e.getRootCause();
//            if (rootCause instanceof SQLException && ((SQLException) rootCause).getErrorCode() == 1452) {
//                throw new IllegalArgumentException("外键冲突，请检查用户ID或停车场ID是否存在", e);
//            } else {
//                throw new IllegalArgumentException(e.getMessage());
//            }
//        }
    }

    @Override
    public void updateParkingSpaceById(ParkingSpaceEntity parkingSpace) {
        ParkingSpacePO parkingSpacePO = parkingSpacePOConvert.convertParkingSpaceEntityToPO(parkingSpace);
        parkingSpaceDao.updateByPrimaryKeySelective(parkingSpacePO);
    }

    @Override
    public ParkingSpaceEntity getParkingSpaceById(Integer id) {
        ParkingSpacePO parkingSpacePO = parkingSpaceDao.selectById(id);
        return parkingSpacePOConvert.convertParkingSpacePOToEntity(parkingSpacePO);
    }

    @Override
    public IPage<ParkingSpaceEntity> getParkingSpacesPage(PageQueryEntity pageQueryEntity) {
        // 根据页码和页面大小,构建分页对象
        Page<ParkingSpacePO> page = new Page<>(pageQueryEntity.getPageIndex(), pageQueryEntity.getPageSize());
        // 采用模糊查询
        IPage<ParkingSpacePO> pageMessage = parkingSpaceDao.selectPageMessageByCondition(
                page,
                pageQueryEntity.getLotCode(),
                pageQueryEntity.getParkingSpaceCode(),
                pageQueryEntity.getIsAvailable(),
                pageQueryEntity.getIsMemberOnly(),
                pageQueryEntity.getUserId(),
                pageQueryEntity.getSortName(),
                pageQueryEntity.getSort()
        );
        return parkingSpacePOConvert.convertParkingSpacePOPageToEntity(pageMessage);
    }

    @Override
    public ParkingSpaceInfoEntity getSpacesAvailableInfoByCode(String lotCode) {
        QueryWrapper<ParkingSpacePO> wrapperAvailable = new QueryWrapper<>();
        wrapperAvailable.eq("lot_code", lotCode).eq("is_available", 1);
        QueryWrapper<ParkingSpacePO> wrapper = new QueryWrapper<>();
        wrapper.eq("lot_code", lotCode);

        return ParkingSpaceInfoEntity.builder()
                .totalSpaces(parkingSpaceDao.selectCount(wrapper))
                .availableSpaces(parkingSpaceDao.selectCount(wrapperAvailable))
                .build();
    }

    @Override
    public ParkingSpaceEntity getParkingSpaceByCode(String lotCode, String spaceCode) {
        QueryWrapper<ParkingSpacePO> wrapper = new QueryWrapper<>();
        wrapper.eq("lot_code", lotCode).eq("code", spaceCode);

        ParkingSpacePO parkingSpacePO = parkingSpaceDao.selectOne(wrapper);
        return parkingSpacePOConvert.convertParkingSpacePOToEntity(parkingSpacePO);
    }
}
